
/**
 * 处理数据请求与接收
 */
(function(){

    var eventManager = require("event_manager");
    var dataManager = {
    };

    dataManager.init = function() {
        this.itemList = [];
        this.transIds = [];
        this.markItems = [];
        
        this.testItemList = [];
    };
    
    dataManager.updateStore = function(itemArray) {
        if (!itemArray) return;
        for (var i = 0; i < itemArray.length; ++i) {
            var tokenid = itemArray[i];
            if (tokenid && tokenid > 0) {
                require("handler").getItemInfo(tokenid);
            }
        }
    };

    dataManager.updateItem = function(itemObj) {
        if (!itemObj) return;
        this.itemList = this.itemList || [];
        for (var i = 0; i < this.itemList.length; ++i) {
            var item = this.itemList[i];
            if (item && item.itemId == itemObj.itemId) {
                this.itemList[i] = itemObj;
                eventManager.fire(eventManager.DATA_CHANGE);
                return;
            }
        }
        this.itemList.push(itemObj);
        eventManager.fire(eventManager.DATA_CHANGE);
    };

    dataManager.getItemList = function() {
        return this.itemList || [];
    };

    dataManager.updateMarketItems = function(transIds) {
        this.transIds = transIds;
    };

    dataManager.getTransIds = function() {
        return this.transIds || [];
    }

    dataManager.addMarketItems = function(sellItem) {
        if (!sellItem) return;
        this.markItems = this.markItems || [];
        for (var i = 0; i < this.markItems.length; ++i) {
            var item = this.markItems[i];
            if (item && item.tokenId == sellItem.tokenId) {
                this.markItems[i] = sellItem;
                return;
            }
        }
        this.markItems.push(sellItem);
        eventManager.fire(eventManager.DATA_CHANGE);
    };

    dataManager.getMarketItems = function() {
        return this.markItems || [];
    };

    module.exports = dataManager;
})();